<?php

/**
 * 智能名片
 */

namespace app\api\controller;

use app\common\controller\ApiController;
use custom\Err;
use think\Exception;
use app\api\model\Agent as Agents;
use app\api\model\AgentAttention;
use app\api\model\AgentRemind;
use app\common\lib\RespCode;

class Agent extends ApiController {

    protected $resubmitLock =['submit'];
    protected $isArrayResult = ['getschool','getaddre','gettag','getrecommendhouse','showcollect'];

    /**
     * 名片个人信息
     */
    public function getAgent() {
        try {
            $params = $this->checkParams ('');
            $info=$this->uInfo;
            $Agent = new Agents();
            $data = $Agent->getAgent ($params,$info);

            return $this->resData ('success', $data);
        } catch (Exception $e) {
            return $this->resErr ($e->getMessage (), $e->getCode ());
        }
    }

    /**
     * 获取高校列表
     * @return false|string
     */
    public function getSchool() {
        try {
            $params = $this->checkParams ('page');
            $Agent = new Agents();
            $data = $Agent->getSchool ($params,20);
            return $this->resData ('success', $data);
        } catch (Exception $e) {
            return $this->resErr ($e->getMessage (), $e->getCode ());
        }
    }

    /**
     * 城市列表
     * @return false|string
     */
    public function getAddre() {
        try {
            $Agent = new Agents();
            $data = $Agent->getAddre();
            return $this->resData ('success', $data);
        } catch (Exception $e) {
            return $this->resErr ($e->getMessage (), $e->getCode ());
        }
    }

    /**
     * 通用标签
     * @return false|string
     */
    public function getTag() {
        try {
            $Agent = new Agents();
            $data = $Agent->getTag();
            return $this->resData ('success', $data);
        } catch (Exception $e) {
            return $this->resErr ($e->getMessage (), $e->getCode ());
        }
    }

    /**
     * 推荐房源
     * @return false|string
     */
    public function getRecommendHouse() {
        try {
            $params = $this->checkParams ('page');
            $Agent = new Agents();
            $pageSize=10;//每页显示数据数
            $data = $Agent->getRecommendHouse ($params,$pageSize);
            return $this->resData ('success', $data);
        } catch (Exception $e) {
            return $this->resErr ($e->getMessage (), $e->getCode ());
        }
    }

    /**
     * 名片信息
     * @return false|string
     */
    public function getCardInfo(){
        try {
            $params = $this->checkParams ('');
            $info=$this->uInfo;
            $params['company_id']=$info['company_id'];
            $Agent = new Agents();
            $data = $Agent->getCardInfo ($params);
            return $this->resData ('success', $data);
        } catch (Exception $e) {
            return $this->resErr ($e->getMessage (), $e->getCode ());
        }
    }

    /**
     * 获取教育经历详情
     * @return false|string
     */
    public function getEducationInfo(){
        try {
            $params = $this->checkParams ('');
            $Agent = new Agents();
            $data = $Agent->getEducationInfo ($params);
            return $this->resData ('success', $data);
        } catch (Exception $e) {
            return $this->resErr ($e->getMessage (), $e->getCode ());
        }
    }


    /**
     * 名片模板样式保存
     * @return false|string
     */
    public function templateSave(){
        try {
            $params = $this->checkParams ('template_id,cover');
            $info=$this->uInfo;
            $Agent = new Agents();
            $re = $Agent->templateSave ($params,$info);
            if ($re){
                return $this->resData ('操作成功', []);
            }else{
                return $this->resErr();
            }
        } catch (Exception $e) {
            return $this->resErr ($e->getMessage (), $e->getCode ());
        }
    }

    /**
     * 个人信息保存
     * @return false|string
     */
    public function agentSave(){
        try {
            $params = $this->checkParams ('');
            $Agent = new Agents();
            $re = $Agent->agentSave ($params);
            if ($re){
                return $this->resData ('操作成功', []);
            }else{
                throw new Exception('操作失败', RespCode::PARAMS_ERR);
            }
        } catch (Exception $e) {
            return $this->resErr ($e->getMessage (), $e->getCode ());
        }
    }

    /**
     * 家乡信息保存
     * @return false|string
     */
    public function homeSave(){
        try {
            $params = $this->checkParams ('city_code');
            $Agent = new Agents();
            $re = $Agent->homeSave ($params);
            if ($re){
                return $this->resData ('操作成功', []);
            }else{
                throw new Exception('操作失败', RespCode::PARAMS_ERR);
            }
        } catch (Exception $e) {
            return $this->resErr ($e->getMessage (), $e->getCode ());
        }
    }

    /**
     * 教育经历信息保存
     * @return false|string
     */
    public function educationSave(){
        try {
            $params = $this->checkParams ('');
            $Agent = new Agents();
            $re = $Agent->educationSave ($params);
            if ($re){
                return $this->resData ('操作成功', []);
            }else{
                return $this->resErr ();
            }
        } catch (Exception $e) {
            return $this->resErr ($e->getMessage (), $e->getCode ());
        }
    }

    /**
     * 保存自我描述
     * @return false|string
     */
    public function describeSave(){
        try {
            $params = $this->checkParams ('');
            $Agent = new Agents();
            $re = $Agent->describeSave ($params);
            if ($re){
                return $this->resData ('操作成功', []);
            }else{
                return $this->resErr ();
            }
        } catch (Exception $e) {
            return $this->resErr ($e->getMessage (), $e->getCode ());
        }
    }

    /**
     * 标签保存
     * @return false|string
     */
    public function tagSave(){
        try {
            $params = $this->checkParams ('');
            $tag_num=isset($params['label'])?count(explode(',',$params['label'])):0;
            if ($tag_num>4){
                Err::add('最多添加4个标签',RespCode::PARAMS_ERR);
                return $this->resErr ();
            }
            $Agent = new Agents();
            $re = $Agent->tagSave ($params);
            if ($re){
                return $this->resData ('操作成功', []);
            }else{
                return $this->resErr ();
            }
        } catch (Exception $e) {
            return $this->resErr ($e->getMessage (), $e->getCode ());
        }
    }

    /**
     * 录音保存
     * @return false|string
     */
    public function voiceSave(){
        try {
            $params = $this->checkParams ('voice,voice_time');

            if ($params['voice_time']>15){
                throw new Exception('录音时长不能超过15秒', RespCode::PARAMS_ERR);
            }
            $Agent = new Agents();
            $re = $Agent->voiceSave ($params);
            if ($re){
                return $this->resData ('操作成功', []);
            }else{
                throw new Exception('操作失败', RespCode::PARAMS_ERR);
            }
        } catch (Exception $e) {
            return $this->resErr ($e->getMessage (), $e->getCode ());
        }
    }

    /**
     * 推荐房源编辑
     * 编辑房源名称、描述；
     * @return false|string
     */
    public function recommendHouseModify(){
        try {
            $params = $this->checkParams ('id');
            $Agent = new Agents();
            $re = $Agent->recommendHouseModify ($params);
            if ($re){
                return $this->resData ('操作成功', []);
            }else{
                return $this->resErr ();
            }
        } catch (Exception $e) {
            return $this->resErr ($e->getMessage (), $e->getCode ());
        }
    }

    /**
     * 照片保存
     * @return false|string
     */
    public function picsSave(){
        try {
            $params = $this->checkParams ('');
            $Agent = new Agents();
            $re = $Agent->picsSave ($params);
            if ($re){
                return $this->resData ('操作成功', []);
            }else{
                throw new Exception('操作失败', RespCode::PARAMS_ERR);
            }
        } catch (Exception $e) {
            return $this->resErr ($e->getMessage (), $e->getCode ());
        }
    }
    /**
     * 经纪人用户信息
     * @return false|string
     */
    public function personalHomePage() {
        try {
            $params = $this->checkParams('');
            if ($params === false) {
                return $this->resErr();
            }
            $Agent = new Agents();
            $re = $Agent->getpersonal($params);
            return $this->resData('success', $re);
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 经纪人修改头像
     * @return false|string
     */
    public function editAvatar() {
        try {
            $params = $this->checkParams('avatar');
            if ($params === false) {
                return $this->resErr();
            }
            $Agent = new Agents();
            $info=$this->uInfo;
            $re = $Agent->editAvatar($params,$info);
            if ($re){
                $this->resData('操作成功', []);
            }else{
                $this->resErr();
            }

        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }


    /**
     * 添加关注或者提醒
     * @param id 收藏id
     * @param c_type 1:关注，2:提醒
     * @param r_type 1：房源，2：小区
     * @return false|string
     */
    public function addCollect() {
        try {
            $params = $this->checkParams('id,c_type,r_type');
            if ($params === false) {
                return $this->resErr();
            }
            //关注
            if ($params['c_type'] == 1) {
                $UserAttention = new AgentAttention();
                $re = $UserAttention->add($params);
                if ($re) {
                    return $this->resData('操作成功', []);
                }
            }
            //提醒
            if ($params['c_type'] == 2) {
                $AgentRemind = new AgentRemind();
                $re = $AgentRemind->add($params);
                if ($re) {
                    return $this->resData('操作成功', []);
                }
            }
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 删除关注或者提醒
     * @param ids 收藏id
     * @param c_type 1:关注，2:提醒
     * @return false|string
     */
    public function delCollect() {
        try {
            $params = $this->checkParams('ids,c_type,r_type');
            if ($params === false) {
                return $this->resErr();
            }
            //关注
            if ($params['c_type'] == 1) {
                $UserAttention = new AgentAttention();
                $re = $UserAttention->del($params);
                return $this->resData('操作成功', []);
            }
            //提醒
            if ($params['c_type'] == 2) {
                $AgentRemind = new AgentRemind();
                $re = $AgentRemind->del($params);
                return $this->resData('操作成功', []);
            }
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }


    /**
     * 显示关注或者提醒房源
     * @param c_type 1:关注，2:提醒
     * @return false|string
     */
    public function showCollect() {
        try {
            $params = $this->checkParams('c_type,r_type');
            if ($params === false) {
                return $this->resErr();
            }
            //关注
            if ($params['c_type'] == 1) {
                $UserAttention = new AgentAttention();
                $re = $UserAttention->show($params);
            }
            //提醒
            if ($params['c_type'] == 2) {
                $AgentRemind = new AgentRemind();
                $re = $AgentRemind->show($params);
            }
            return $this->resData('success', $re);
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 经纪人问题反馈
     * @return false|string
     */
    public function problemSubmit() {
        try {
            $params = $this->checkParams('content');
            if ($params === false) {
                return $this->resErr();
            }
            $Agent = new Agents();
            $re = $Agent->addProblem($params);
            if (!$re) {
                throw new Exception('操作失败', RespCode::QUERY_ERR);
            } else {
                return $this->resData('操作成功', []);
            }
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }
}
